﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using IOEntity;
namespace IODataAccess
{
    public class clsDAAdmin
    {
        clsDAConnection con = new clsDAConnection();
        public clsAdmin GetByUserPassword(string username,string password)
        {
            try
            {
                clsAdmin obj = new clsAdmin();
                string str = "select * from tblAdmin where tendangnhap=@tendangnhap and matkhau=@matkhau";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tendangnhap", username);
                com.Parameters.AddWithValue("@matkhau", password);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    if (dr["ten"] != DBNull.Value)
                    {
                        obj.Ten = dr["ten"].ToString();
                    }
                    if (dr["tendangnhap"] != DBNull.Value)
                    {
                        obj.TenDangNhap = dr["tendangnhap"].ToString();
                    }
                    if (dr["matkhau"] != DBNull.Value)
                    {
                        obj.MatKhau = dr["matkhau"].ToString();
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public clsAdmin GetById(int id)
        {
            try
            {
                clsAdmin obj = new clsAdmin();
                string str = "select * from tblAdmin where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    obj.Id = id;
                    if (dr["ten"] != DBNull.Value)
                    {
                        obj.Ten = dr["ten"].ToString();
                    }
                    if (dr["tendangnhap"] != DBNull.Value)
                    {
                        obj.TenDangNhap = dr["tendangnhap"].ToString();
                    }
                    if (dr["matkhau"] != DBNull.Value)
                    {
                        obj.MatKhau = dr["matkhau"].ToString();
                    }
                    
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsAdmin> GetAll()
        {
            try
            {
                List<clsAdmin> list = new List<clsAdmin>();
                string str = "select * from tblAdmin";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsAdmin obj = new clsAdmin();
                    if (dr["ten"] != DBNull.Value)
                    {
                        obj.Ten = dr["ten"].ToString();
                    }
                    if (dr["tendangnhap"] != DBNull.Value)
                    {
                        obj.TenDangNhap = dr["tendangnhap"].ToString();
                    }
                    if (dr["matkhau"] != DBNull.Value)
                    {
                        obj.MatKhau = dr["matkhau"].ToString();
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int Insert(clsAdmin obj)
        {
            try
            {
                string str = "insert into tblAdmin(ten,tendangnhap,matkhau) values(@ten,@tendangnhap,@matkhau)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@ten", obj.Ten);
                com.Parameters.AddWithValue("@tendangnhap", obj.TenDangNhap);
                com.Parameters.AddWithValue("@matkhau", obj.MatKhau);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblAdmin");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsAdmin obj)
        {
            try
            {
                string str = "Update tblAdmin set ten=@ten,tendangnhap=@tendangnhap,matkhau=@matkhau where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@ten", obj.Ten);
                com.Parameters.AddWithValue("@tendangnhap", obj.TenDangNhap);
                com.Parameters.AddWithValue("@id", obj.Id);
                com.Parameters.AddWithValue("@matkhau", obj.MatKhau);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblAdmin where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
